//
//  singleton.m
//  XRmenu
//
//  Created by shrek on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "singleton.h"
@implementation singleton
@synthesize language, uistyle, currentBookID, currentOrderNo;
@synthesize allconfig, translate;
@synthesize dishdata, filteredListContent;
@synthesize bgMusic;
@synthesize relateDishLayerStatus;
@synthesize currentBatchNo;

@synthesize pluMenus, myOrders, pluPlus,pluSorts, currentPluPlu, currentPluIndex,currentMenuIndex,currentSortIndex,lastPluIndex,allCommendPlu,allTables,isViewRemoveFromLef,isAnimating,udid;

+(singleton *)sharedSingleton {
	static singleton *sharedSingleton;
	
	@synchronized(self)
	{
		if (!sharedSingleton) {
			if (!sharedSingleton)
				sharedSingleton=[[singleton alloc] init];
		}
		return sharedSingleton;
	}
}

#pragma mark XRmenu
-(void) getAllMyOrder{
    if (self.myOrders != nil && [self.myOrders count]>0) {
        [self.myOrders removeAllObjects];
    }
    self.myOrders=[XRmenuCoreData getAllDishOrder];
}
-(void) initPluMenus{
    self.allCommendPlu=[XRmenuCoreData getAllCommendPlu];//初始化推荐菜
    self.allTables=[XRmenuCoreData getAllPluTable];//初始化桌号
    
    self.pluMenus=[XRmenuCoreData getAllPluMenu];
    [self updateCurrentMenuIndex:0];//默认选中第一个大类
}
-(void) updateCurrentMenuIndex:(NSInteger) newCurrentMenuIndex{
    if (newCurrentMenuIndex<0) {
        DebugLog(@"小于0!newCurrentIndex=%d", newCurrentMenuIndex);
        return;
    }
    
    //重新更新小类对象数组
    if (self.pluSorts != nil && [self.pluSorts count]>0) {
        [self.pluSorts removeAllObjects];
    }
    if ([self.pluMenus count]>0) {
        for (id sortObj in ((PluMenu *)[self.pluMenus objectAtIndex:newCurrentMenuIndex]).rsToPluSort) {
            [self.pluSorts addObject:(PluSort *) sortObj];
        }
    }
    
    //小类排序
    NSSortDescriptor *sortByPsSort=[NSSortDescriptor sortDescriptorWithKey:@"psSort" ascending:YES];
    [self.pluSorts sortUsingDescriptors:[NSArray arrayWithObject:sortByPsSort]];
    
    //更新当前大类的序号
    self.currentMenuIndex=newCurrentMenuIndex;
    [self updateCurrentSortIndex:0];//默认显示第一小类下的菜品
}

-(void) updateCurrentSortIndex:(NSInteger) newCurrentSortIndex{
//    if (newCurrentSortIndex < 0) {
//        DebugLog(@"小于0!newCurrentIndex=%d", newCurrentSortIndex);
//        return;
//    }
    if (self.pluPlus != nil && [self.pluPlus count]>0) {
        [self.pluPlus removeAllObjects];
    }
    if (newCurrentSortIndex>=0 && [self.pluSorts count]>0) {
        for (id pluObj in ((PluSort *)[self.pluSorts objectAtIndex:newCurrentSortIndex]).rsToPluPlu) {
            PluPlu *tempPlu=(PluPlu *)pluObj;
            if ([tempPlu.ppStatus intValue] == 1) {
                [self.pluPlus addObject:tempPlu];
            }
        }
    }
    
    //菜品排序
    NSSortDescriptor *sortByPpCode=[NSSortDescriptor sortDescriptorWithKey:@"ppCode" ascending:YES];
    [self.pluPlus sortUsingDescriptors:[NSArray arrayWithObject:sortByPpCode]];
    
    self.currentSortIndex=newCurrentSortIndex;
    self.lastPluIndex=0;
    [self updateCurrentPluIndex:0];//默认选中第一个菜品
}

-(void) updateCurrentPluIndex:(NSInteger) newCurrentIndex{
    self.currentPluIndex=newCurrentIndex;
    if (newCurrentIndex<0) {
        DebugLog(@"小于0!newCurrentIndex=%d", newCurrentIndex);
        self.currentPluPlu=nil;
        return;
    }
    if (newCurrentIndex>=[self.pluPlus count]) {
        DebugLog(@"超出边界!newCurrentIndex=%d,maxIndex=%d", newCurrentIndex,[self.pluPlus count]-1);
        self.currentPluPlu=nil;
        return;
    }
    self.currentPluPlu=(PluPlu *)[self.pluPlus objectAtIndex:newCurrentIndex];//用于大图显示
}

#pragma mark -
-(NSMutableArray*)getDataWithKey:(NSString*)aString
{	
	if ([aString length]==0) {
		return nil;
	} 
	NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.pEntityId CONTAINS %@", aString];
	NSMutableArray* res = [NSMutableArray arrayWithArray:[self.dishdata filteredArrayUsingPredicate:filterPredicate]];
	DebugLog(@"过滤后的数据:%@",res);
	self.dishdata = res;
	return res;
}
-(NSMutableArray*)getDataWithSort:(NSInteger)aSort sortId:(NSString*)aSortId
{
	if (aSort==-1) {
		
	}else if (aSort==0) {
		 //大类  
		NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"rsToCatalogs.cCatalogsId == '%@'", aSortId];
		NSMutableArray *res = [XRmenuCoreData searchObjectsInContext:@"Product" Predicate:filterPredicate SortKey:@"pID" SortAscending:YES];
		DebugLog(@"过滤后的数据:%@",res);
		self.dishdata = res;
		
	} 
	else if (aSort==2) {
		//标签
		NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF.pEntityId CONTAINS %@", aSortId];
		NSMutableArray* res = [NSMutableArray arrayWithArray:[self.dishdata filteredArrayUsingPredicate:filterPredicate]];
		DebugLog(@"过滤后的数据:%@",res);
		self.dishdata = res;
	} 
	return self.dishdata;
}

// 播放音乐
-(void) playSoundOnEvent:(NSString *)mp3File{
	NSURL *fileURL = [NSURL fileURLWithPath:mp3File];
    
//    [bgMusic initWithContentsOfURL:fileURL error:nil];
//	[bgMusic prepareToPlay];
//    [bgMusic setVolume:0.7];
//    [bgMusic setNumberOfLoops:0];
//    [bgMusic setDelegate:nil];
//	[bgMusic play];
    
    AVAudioPlayer *testMusic=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
	[testMusic prepareToPlay];
    [testMusic setVolume:0.7];
    [testMusic setNumberOfLoops:0];
    [testMusic setDelegate:nil];
	[testMusic play];
    while ([testMusic isPlaying]) {
        [NSThread sleepForTimeInterval:0.1f];//这里必须sleep 否则报错!
    }
    [testMusic release];
}

-(void) stopSoundOnEvent
{
	[bgMusic stop];
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)playedPlayer successfully:(BOOL)flag {
	//[[NSNotificationCenter defaultCenter] postNotificationName:kMessage_AudioPlayer_Stop object:self userInfo:nil];
}

-(id) init
{
	self = [super init];
	if (self) {
		NSString* cfile = [XRmenuFileManage getViewConfigFile];
		self.allconfig = [NSDictionary dictionaryWithContentsOfFile:cfile];
		NSString* tfile = [XRmenuFileManage getTranslate];
		self.translate = [NSDictionary dictionaryWithContentsOfFile:tfile];
		self.currentBatchNo = 0;
		
		NSString* sfile = [XRmenuFileManage savedataFile:@"search_history"];
		if (sfile) {
			self.filteredListContent = [NSMutableArray arrayWithContentsOfFile:sfile];
		}
		else {
			self.filteredListContent = [[NSMutableArray new] autorelease];
		}
		self.relateDishLayerStatus=1;  //我加地
		bgMusic=[[AVAudioPlayer alloc] init];
        
        pluMenus=[[NSMutableArray alloc] init];
        pluPlus=[[NSMutableArray alloc] init];
        pluSorts=[[NSMutableArray alloc] init];
        myOrders=[[NSMutableArray alloc] init];
        allCommendPlu=[[NSMutableArray alloc] init];
        allTables=[[NSMutableArray alloc] init];
        
        udid=@"no uuid!";        
        isViewRemoveFromLef=YES;
        isAnimating=NO;
	}
	return self;
}

//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
    [uistyle release];
    uistyle = nil;
    [language release];
    language = nil;
	[currentBookID release];
	currentBookID = nil;
	[currentOrderNo release];
	currentOrderNo = nil;
	
    [allconfig release];
    allconfig = nil;
	[translate release];
	translate = nil;
	[dishdata release];
    dishdata = nil;
    [myOrders release];
    myOrders=nil;
    [pluMenus release];
    pluMenus=nil;
    [pluSorts release];
    pluSorts=nil;
    [udid release];
    udid=nil;
    
    [pluPlus release];
    pluPlus=nil;
    [allCommendPlu release];
    allCommendPlu=nil;
    [allTables release];
    allTables=nil;
    
	[filteredListContent release];
	filteredListContent = nil;
	
	[bgMusic release];
    bgMusic=nil;
	
    [super dealloc];
}
@end
